package com.gemme.gemme.wode;

import android.media.Image;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.adapter.DataBean;
import com.gemme.gemme.adapter.ImageAdapter;
import com.gemme.gemme.util.DpUtil;
import com.gemme.gemme.util.StrUtil;
import com.gemme.gemme.view.ChatDialog;
import com.gemme.gemme.view.ProShaidanBean;
import com.gemme.gemme.view.Topview;
import com.google.android.material.tabs.TabLayout;
import com.jaeger.library.StatusBarUtil;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import com.youth.banner.Banner;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

//侧滑删除地址
public class ShaidanActivity extends BaseActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shaidan);
        initTop();
        initView();
        initIntentData();
    }

    private void initIntentData() {
        try {
            String data = getIntent().getStringExtra("data");
            ProShaidanBean proShaidanBean = JSON.parseObject(data, ProShaidanBean.class);
            List<ProShaidanBean.DataBean.RecordsBean> records = proShaidanBean.getData().getRecords();
//            setData(records);
            System.out.println();
            adapter = new AddressNewAdapter(records);
            recyclerView.setAdapter(adapter);
        }catch (Exception e){

        }
    }


    AddressNewAdapter adapter;
    SwipeRecyclerView recyclerView;
    ArrayList<String> mDataList = new ArrayList<>();
    private void initView() {

        recyclerView = findViewById(R.id.rvResult);

        mDataList.add("1");
        mDataList.add("2");
        mDataList.add("3");
        mDataList.add("4");
       /* DefaultItemDecoration mItemDecoration = new DefaultItemDecoration(ContextCompat.getColor
                (this, R.color.divider_color));
        recyclerView.addItemDecoration(mItemDecoration);*/
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLayoutManager);
//        recyclerView.setLongPressDragEnabled(false); // 拖拽排序，默认关闭。
       /* adapter = new AddressNewAdapter();
        recyclerView.setAdapter(adapter);*/


    }



    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("买家晒单");
    }



    public class AddressNewAdapter extends RecyclerView.Adapter<AddressNewAdapter.ViewHolder>{

        private List<ProShaidanBean.DataBean.RecordsBean> adapterList;

        //List<ProShaidanBean.DataBean.RecordsBean> records
        public AddressNewAdapter(List<ProShaidanBean.DataBean.RecordsBean> adapterList){

            this.adapterList = adapterList;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
            //adapter_address
            View inflate = LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.shaidan_item, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            inflate.setLayoutParams(layoutParams);
            return new AddressNewAdapter.ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            try {
//                banner.
                ProShaidanBean.DataBean.RecordsBean recordsBean = adapterList.get(position);
                holder.tv_content.setText(StrUtil.noNUll(recordsBean.getCommentContent()));
                holder.tv_dianzan.setText(StrUtil.noNUll(recordsBean.getFabulous()));
                holder.tv_pinglinnum.setText(StrUtil.noNUll(recordsBean.getCommcount()));
//                holder.iv_head
                if(!StrUtil.isNUll(recordsBean.getHeadImgUrl()))
                Glide.with(mContext).load(recordsBean.getHeadImgUrl() + "").into(holder.iv_head);
                if(recordsBean.getNickname()!=null){
                    holder.tv_name.setText(StrUtil.noNUll(recordsBean.getNickname()));
                }else {
                    String text = recordsBean.getUsername();
                    String sub_str = null;
                    sub_str = text.substring(3, 7);
                    text = text.replace(sub_str, "****");
                    holder.tv_name.setText(StrUtil.noNUll(text));
                }
                holder.tv_guige.setText("规格:"+recordsBean.getSpecValue());
//                holder.tv_pinglinnum.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        ProductChatDialog productChatDialog = new ProductChatDialog();
//                        productChatDialog.showDialog(mContext);
//                        productChatDialog.setContentId(JSON.toJSONString(recordsBean));
//                    }
//                });

              /*  holder.tv_pinglun1.setText(StrUtil.noNUll(recordsBean.get));
                holder.tv_pinglun2.setText(StrUtil.noNUll(recordsBean.));*/



//        CoordinatorLayout
               initBanner(recordsBean,holder);
            }catch (Exception e){

            }
        }

        private void initBanner(ProShaidanBean.DataBean.RecordsBean recordsBean,ViewHolder holder) {
            ArrayList<DataBean> dataBeans = new ArrayList<>();
            List<String> picList = recordsBean.getPictureList();
            if(picList!=null&&picList.size()!=0){
                holder.banner.setVisibility(View.VISIBLE);
                for (int i = 0; i < picList.size(); i++) {
                    DataBean dataBean = new DataBean();
                    dataBean.setImageRes(picList.get(i));
                    dataBeans.add(dataBean);
                }
            }else {
                holder.banner.setVisibility(View.GONE);
            }

            holder.banner
//                .addBannerLifecycleObserver(this)//添加生命周期观察者
                    .setAdapter(new ImageAdapter(dataBeans))
                    .setIndicator(new CircleIndicator(mContext))
//                .setIndicator(new RectangleIndicator(this))
                    .isAutoLoop(false)
                    .setIndicatorMargins(new IndicatorConfig.Margins(0,0,0, DpUtil.dp2px(mContext,50)))
                    .start();
        }

        @Override
        public int getItemCount() {
            return adapterList.size();
        }


        public class ViewHolder extends RecyclerView.ViewHolder {
            Banner banner;
            ImageView iv_head;
            TextView tv_name;
            TextView tv_content;
            TextView tv_guige;
            TextView tv_dianzan;
            TextView tv_pinglinnum;
            TextView tv_pinglun1;
            TextView tv_pinglun2;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                iv_head = itemView.findViewById(R.id.iv_head);
                banner = itemView.findViewById(R.id.banner);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_content = itemView.findViewById(R.id.tv_content);
                tv_guige = itemView.findViewById(R.id.tv_guige);
                tv_dianzan = itemView.findViewById(R.id.tv_dianzan);
                tv_pinglinnum = itemView.findViewById(R.id.tv_pinglinnum);
                tv_pinglun1 = itemView.findViewById(R.id.tv_pinglun1);
                tv_pinglun2 = itemView.findViewById(R.id.tv_pinglun2);

            }
        }
    }

}